<template>
  <div class="stick-particle">
    <div class="canvas" ref="canvas"></div>
  </div>
</template>

<script lang="ts">
import { Vue, Options } from 'vue-property-decorator';
import * as THREE from 'three';
import logo from '../assets/logo.png';
const logoSrc = import.meta.globEager('../assets/*.png');
import stick from '../libs/stick.json';
const scene =  new THREE.Scene();
const camera = new THREE.PerspectiveCamera(80, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.set(0, 0, 40);
scene.add(camera);
const point  = new THREE.PointLight(0xffffff);
point.position.set(400, 200, 300);
scene.add(point);
const ambient = new THREE.AmbientLight(0x444444);
scene.add(ambient);
const renderer = new THREE.WebGLRenderer({
  alpha: true,
  antialias: true,
});
renderer.debug.checkShaderErrors = false;
renderer.setClearColor(new THREE.Color(), 0);
renderer.setPixelRatio(750 / window.innerWidth);


@Options({ components: {} })
export default class StickParticle extends Vue {
  public rect: DOMRect|null = null;
  public count = 1;

  public mounted() {
    this.rect = (this.$el as HTMLElement).getBoundingClientRect();
    renderer.setSize(this.rect.width, this.rect.height, false);
    (this.$refs.canvas as HTMLDivElement).append(renderer.domElement);
    const texture = new THREE.TextureLoader().load(logoSrc['../assets/snow1.png'].default);

    const group = new THREE.Group();
    for (let i = 0;i < 100;i++) {
      const spriteMaterial = new THREE.SpriteMaterial({
        color: 0xff00ff,
        map: texture,
      });
      const sprite = new THREE.Sprite(spriteMaterial);
      const x = Math.random() - 0.5 ;
      const y = Math.random() - 0.5 ;
      sprite.name =  `${i}`;
      sprite.scale.set(0.5, 0.5, 1);
      sprite.position.set(x * 100, y * 100, 1);
      group.add(sprite);
    }
    scene.add(group);

    renderer.setAnimationLoop(() => {
      group.children.forEach((sprite) => {
        sprite.position.x = (Math.random() - 0.5) * 100;
        sprite.position.y = (Math.random() - 0.5) * 100;
      });
      renderer.render(scene, camera);
    });
  }
}
</script>

<style lang="postcss" scoped>
  .stick-particle{
    touch-action: none;
    position: absolute;
    left: 0;top: 0;
    width: 100%;height: 100%;
    background: #1c1c1c;
 .canvas{
      position: absolute;
      left: 0;top: 0;
      width: 100%;height: 100%;
       :deep(canvas){
        width: 100%;height: 100%;
      }
    }
  }
</style>
